#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail  # needed for snip

# We are started in the doc/ dir, but let's use the parent dir.
readonly ANNEX_ROOT=$(cd .. && pwd)

readonly THIS_DIR=$PWD  # Save current directory before changing it

# TODO: Build date?
# TODO: Source editing date has to come from the text file.  Just use
# stat; and then pin --data foo.
jsont() {
  $ANNEX_ROOT/_tmp/deps/jsont.tin "$@"
}

pin() {
  $ANNEX_ROOT/_tmp/deps/pin.tin "$@"
}

snip() {
  $ANNEX_ROOT/_tmp/deps/snip.tin "$@"
}


# Print the mod time of a file so we can expose it to HTML.  TODO: This should
# be in annex.
modtime-str() {
  local file=$1
  # sed gets rid of the extra precision in: 1:40:54.234234234
  stat --format '%y' $file | sed 's/\.[0-9]\+//'
}

make-dict() {
  local name="$1"
  local modtime="$2"
  # Add PULP_ variables from the environment, so that 'PULP_latch=1 poly build'
  pin \
    --env-prefix PULP_ \
    --data Pin \
    --add "source-modtime: $modtime" \
    --title-tag=h1 \
    $SCRATCH/$name.html
}

# Add the HTML shell.  A data dictionary should be readable stdin.  Does NOT
# depend on $PWD, so callers can cd.
to-html() {
  local name=$1
  jsont $THIS_DIR/html.jsont > $OUT/$name
}

markdown-out() {
  local name=$1
  echo markdown $name

  local modtime=$(modtime-str $name.txt)
  # Convert body by first expanding snippets, and then running it through markdown.
  # markdown allows literal HTML, so in most cases there doesn't need to be any
  # escaping.
  cat $name.txt \
    | snip --script $THIS_DIR/Snip - \
    | markdown \
    | sed -e 's|<pre><code>|<pre>|g' -e 's|</code></pre>|</pre>|g' \
    > $SCRATCH/$name.html

  # TODO: make-dict should have a debug mode.  In that case it will add
  # reload-latch: true
  # and then the HTML will include latch.js.  after the page loads, then this
  # will do a GET, and after that succeeds, it will reload().

  make-dict $name "$modtime" | to-html $name.html
}

# Create pygments CSS.
pygments-css() { 
  local style=vs  # vs is minimal.  doesn't have italics for comments.
  local style=tango  # operators too noisy.
  local style=colorful  # string has clashing background
  #local style=native  # doesn't work with grey
  local style=murphy  # string has clashing background
  local style=pastie  # char literals too strong
  local style=borland  # boring
  local style=vim  # garish
  local style=friendly  # operators not emphasized enough.

  pygmentize -S $style -f html > $OUT/code.css
}

build() {
  # TODO: Use multi ln?  Then you can change js/css easily.
  cp App $OUT
  cp doc.css $OUT
  cp toc.js $OUT
  cp latch.js $OUT

  pygments-css

  markdown-out cre-examples
  markdown-out intro
  markdown-out cre-syntax  # Big table
  markdown-out index
  markdown-out url-capture
  markdown-out design
  markdown-out implementation
  markdown-out regex-wrong

  # TODO: TPE syntax highlighting
  # Annotate grammar with more examples.  And it should be SORTED in a decent
  # order.
  markdown-out cre-parser
  # TODO: format help as HTML?  It's already parsed sort of.
  markdown-out annex-command
}

build
